package com.pm.service.awardPunishment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pm.entity.AwardPunishmentElse;
import com.pm.entity.po.file.FileLib;
import com.pm.entity.po.security.User;
import com.pm.mapper.awardPunishment.AwardPunishmentElseMapper;
import com.pm.mapper.file.FileLibMapper;
import com.pm.service.awardPunishment.AwardPunishmentElseService;
import com.pm.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class AwardPunishmentElseServiceImpl implements AwardPunishmentElseService {
    @Autowired
    AwardPunishmentElseMapper awardPunishmentElseMapper;
    @Autowired
    FileLibMapper fileLibMapper;

    @Override
    public PageInfo<AwardPunishmentElse> getAllAwardElse(AwardPunishmentElse awardPunishmentElse) {
        PageHelper.startPage(awardPunishmentElse.getPageNum(), awardPunishmentElse.getPageSize());
        PageInfo<AwardPunishmentElse> pageInfo = new PageInfo<>(awardPunishmentElseMapper.getAllAwardElse(awardPunishmentElse));
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public Integer addAwardElse(AwardPunishmentElse awardPunishment, User user) {
        FileLib fileLib = new FileLib();
        awardPunishment.setAddTime(new Date());
        awardPunishment.setUpdateTime(new Date());
        awardPunishment.setMonth(DateUtil.getMonth(awardPunishment.getOccurrenceTime()));
        awardPunishment.setYear(DateUtil.getYear(awardPunishment.getOccurrenceTime()));
        Integer awardPunishment1 = awardPunishmentElseMapper.addAwardElse(awardPunishment);
        if (awardPunishment.getFiles().size() != 0) {
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(awardPunishment.getFiles());
            fileLib.setAimsId(awardPunishment.getId());
            fileLib.setType(awardPunishment.getFileLibType());
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return awardPunishment1;
    }

    @Override
    public AwardPunishmentElse getAwardElseId(Integer id) {
        return awardPunishmentElseMapper.getAwardElseId(id);
    }

    @Override
    public Boolean deleteAwardElse(Integer id) {
        return awardPunishmentElseMapper.deleteAwardElse(id);
    }


    @Override
    public Boolean updateAwardElse(AwardPunishmentElse awardPunishment, User user) {
        if (awardPunishment.getFiles().size() != 0) {
            FileLib fileLib = new FileLib();
            fileLib.setUserId(user.getUserId());
            fileLib.setFiles(awardPunishment.getFiles());
            fileLib.setAimsId(awardPunishment.getId());
            fileLib.setType(awardPunishment.getFileLibType());
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return awardPunishmentElseMapper.updateAwardElse(awardPunishment);
    }
}
